//
//  MyViewController.m
//  Lmm_IOS
//
//  Created by 张哲炜 on 2018/9/14.
//  Copyright © 2018年 lmm. All rights reserved.
//

#import "MyViewController.h"

@interface MyViewController (){
    
    UIView* headerView;
    UIImageView* headerImageView;
    UIImageView* avatarImageView;
    UILabel* nicknameLabel;
    UITableView* tableView;
}

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
  
    //注册监听登录事件的观察者
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginEvent)name:@"LoginEvent" object:nil];
    
    self.view.backgroundColor=[UIColor whiteColor];
    [self drawView];
  //试图加载完成时调用initUser方法，使app再打开时在登录界面即可显示头像和名字
     [self initUser];
}

- (void) loginEvent {

    [self initUser];
    
}

//将用户头像和图片显示到view 上
- (void) initUser {
    
   UserModel* user = [[PINCache sharedCache]objectForKey:@"user"];
    
    if (user.id != 0) {
        [self->avatarImageView sd_setImageWithURL:[NSURL URLWithString:user.avatar] placeholderImage:[UIImage imageNamed:@"avatar"] options:SDWebImageRefreshCached];
        [self->nicknameLabel setText:user.nickname];
    } else {
        self->avatarImageView.image = [UIImage imageNamed:@"avatar"];
        self->nicknameLabel.text = @"尚未登录";
    }
    
}


-(void)drawView{
    
    headerView =UIView.new;
    [self.view addSubview:headerView];
    
    [headerView makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.equalTo(self.view.left).offset(0);
        make.right.equalTo(self.view.right).offset(0);
        make.height.equalTo(MAIN_SCREEN_WIDTH * 8/15);
    }];
    
    
    headerImageView = UIImageView.new;
    [headerImageView setImage:[UIImage imageNamed:@"my_bg"]];
    [headerView addSubview:headerImageView];
    
    [headerImageView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self->headerView.left).offset(0);
        make.right.equalTo(self->headerView.right).offset(0);
        make.top.equalTo(self->headerView.top).offset(0);
        make.bottom.equalTo(self->headerView.bottom).offset(0);
        
    }];
    
    
    self->avatarImageView = UIImageView.new;
    self->avatarImageView.layer.masksToBounds = YES;
    self->avatarImageView.layer.cornerRadius = 35;
    //表示按比例缩放并且填满view，意味着图片可能超出view，可能被裁减掉
    self->avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    //将超出部分裁掉
    self->avatarImageView.clipsToBounds = YES;
    //self->avatarImageView.image = [UIImage imageNamed:@"avatar"];
    [headerView addSubview:self->avatarImageView];
    [self->avatarImageView makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(70);
        make.width.equalTo(70);
        make.centerX.equalTo(self->headerView.centerX);
        make.centerY.equalTo(self->headerView.centerY);
    }];
    
    
    self->nicknameLabel = UILabel.new;
    //self->nicknameLabel.text = @"尚未登录";
    self->nicknameLabel.font = [UIFont systemFontOfSize:16];
    self->nicknameLabel.textColor = UIColorFromRGB(0xFFFFFF);
    [headerView addSubview:self->nicknameLabel];
    
    [self->nicknameLabel makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self->avatarImageView.bottom).offset(10);
        make.centerX.equalTo(self->avatarImageView.centerX);
    }];
    
    
    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.layer.cornerRadius=35;
    button.layer.masksToBounds = YES;
    [headerView addSubview:button];
    [button addTarget:self action:@selector(jump) forControlEvents:(UIControlEventTouchUpInside)];
    [ button makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(70);
        make.width.equalTo(70);
        make.center.equalTo(self->headerView.center);
        
    }];
    
    
    tableView =UITableView.new;
    tableView.backgroundColor = [UIColor whiteColor];
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    tableView.delegate = self;
    tableView.dataSource = self;
    
    [self.view addSubview:tableView];
    
    [tableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left);
        make.right.equalTo(self.view.right);
        make.top.equalTo(self->headerView.bottom);
        make.bottom.equalTo(self.view.bottom);
    }];
    
    
    

    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 62;
    }

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(indexPath.row == 4){
        FeedBackViewController *feedbackViewController = FeedBackViewController.new;
        feedbackViewController.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:feedbackViewController animated:YES];
        
    }
    
    
}

- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    
    static     NSString* CellIdentifier =@"MyTableViewCell";
    
    MyTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    // 如果取出的表格行为nil
    if (cell == nil) {
        //创建一个UITableViewCell对象，并绑定到cellID
        cell = [[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        }
     [cell drawView:(int)indexPath.row];
    
    return cell;
}


- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 6;
}


-(void)jump{
    
    UserModel*user = [[PINCache sharedCache]objectForKey:@"user"];
    
    if (user.id == 0) {
    
        //点击按钮跳转页面
        LoginViewController* loginVC = [[LoginViewController alloc]init];
        //设置跳转方式
        loginVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:loginVC animated:YES];
        return;
    }
 
   //在已登陆状态时，点击头像弹出提示框
    UIActionSheet* actionSheet = [[UIActionSheet alloc]
                                  initWithTitle:nil
                                  delegate:self
                                  cancelButtonTitle:@"取消"
                                  destructiveButtonTitle:@"退出登录"
                                  otherButtonTitles:@"拍照", @"从相册上传", nil];
    [actionSheet showInView:self.view];
    
}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex == 0) {
        
        [self logout];
        
    } else if (buttonIndex == 1) {
        
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self presentViewController:imagePicker animated:YES completion:nil];
        
    } else if (buttonIndex == 2) {
        UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
        imagePicker.delegate = self;
        imagePicker.allowsEditing = YES;
        imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        [self presentViewController:imagePicker animated:YES completion:nil];
    }
}

#pragma mark ---UIImagePickerController Delegate 选中图片后执行更换头像的函数
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    if ([[info objectForKey:UIImagePickerControllerMediaType] isEqualToString:(__bridge NSString *)kUTTypeImage]) {
        
        //NSLog(@"didFinishPickingMediaWithInfo");
        UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
        
        //[self updateUserAvatar:img];
        //选中图片后执行更换头像的函数
        //[self performSelector:@selector(updateUserAvatar:)  withObject:img afterDelay:0.5];
    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
}


- (void) logout {
    
    [HttpHelper request:@"user/logout"
                       :@"POST"
                       :nil
           successBlock:^(NSDictionary *responseDic) {
               
               
               UserModel * user = [UserModel mj_objectWithKeyValues:[responseDic objectForKey:@"data"]];
               
               
                [[PINCache sharedCache] setObject:user forKey:@"user"];
               //发通知
               [[NSNotificationCenter defaultCenter] postNotificationName:@"LoginEvent" object:self userInfo:nil];
               
           } failureBlock:^(NSError *error) {
               
           }];
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */



@end
